
#include "Main.h"

// My header
#include "SDLEventSystem.h"

// Fridge
#include "Visuals/Visuals_Main.h"
#include "Controller/Controller_Main.h"

u_int SDLEventSystem::s_uControllerWindowID = 0xffffffff;
u_int SDLEventSystem::s_uVisualsWindowID = 0xffffffff;

void SDLEventSystem::Initialise()
{
	s_uControllerWindowID = Controller_Main::GetWindowID();
	s_uVisualsWindowID = Visuals_Main::GetWindowID();
}

void SDLEventSystem::Shutdown()
{

}

void SDLEventSystem::Update()
{
	SDL_PumpEvents();

	SDL_Event xEvent;
	while( SDL_PollEvent( &xEvent ) )
	{
		switch( xEvent.type )
		{
			// All events which have a window id
			case SDL_MOUSEMOTION:
			case SDL_MOUSEBUTTONDOWN:
			case SDL_MOUSEBUTTONUP:
			case SDL_MOUSEWHEEL:
			case SDL_TEXTINPUT:
			case SDL_TEXTEDITING:
			case SDL_WINDOWEVENT:
			case SDL_KEYDOWN:
			case SDL_KEYUP:
			{
				SendToHandler( xEvent, xEvent.window.windowID );
			}
			break;

			// Events which don't have a window id
			case SDL_QUIT:
			{
				// TODO: Quit
			}
			break;
		}
	}
}

void SDLEventSystem::SendToHandler( const SDL_Event& xEvent, u_int uWindowID )
{
	bool bEventHandled = false;

	if( uWindowID == s_uControllerWindowID )
	{
		bEventHandled = Controller_Main::HandleEvent( xEvent );
	}
	else if( uWindowID == s_uVisualsWindowID )
	{
		bEventHandled = Visuals_Main::HandleEvent( xEvent );
	}

	if( bEventHandled )
	{
		return;
	}

	// TODO: FHandle unhandled events
}

//eof
